<?php
class admin_listController extends adminController{

    //首页
    public function index() {
        $this->list=model('site_manage')->site_list();
        $this->show();  
    }

    //添加
    public function add() {
        $this->action_name='添加';
        $this->action='add';
        $this->show('admin_list/info'); 
    }

    public function add_save() {
    	$data=in($_POST);
    	//判断站点重复
    	$repeat_dir=ROOT_PATH.'inc/site/'.$data['SITE_MARK'];
    	if(is_dir($db_dir)){
    		$this->msg('站点已存在，无法建立重复站点！',0);
    	}
    	if($data['data']){
	        //检查数据库
	        $link=@mysql_connect($data['DB_HOST'].':'.$data['DB_PORT'],$data['DB_USER'],$data['DB_PWD']);
			if(!$link){
				$this->msg('数据库连接失败，请检查连接信息是否正确！',0);
			}
			$status=@mysql_select_db($data['DB_NAME'],$link);
			if(!$status){
				$this->msg('数据库不存在请先建立数据库！',0);
			}

    		//导入基础数据
	        $data['DB_PREFIX']=$data['SITE_MARK'].'_dux_';
	        $db_frame=BASE_PATH.'apps/install/data/frame.sql';
			$db_data=BASE_PATH.'apps/install/data/data.sql';
			$sql_frame=Install::mysql($db_frame,'dux_',$data['DB_PREFIX']);
			$sql_data=Install::mysql($db_data,'dux_',$data['DB_PREFIX']);

			if(!model('site_manage')->run_sql($data,$sql_frame)){
	            $this->msg('表结构导入失败，请检查后手动删除数据后重新建立！',0);
			}
			if(!model('site_manage')->run_sql($data,$sql_data)){
				$this->msg('基础数据入失败，请检查后手动删除数据后重新建立！',0);
			}

			//导入APP数据
	        $app_list=getApps();
	        $app_config_array=array();
	        $app_no_list=array('admin','admin_user','advertising','app_manage','content','default','editor','expand_model','filter','form','install','jump','login','pages','recommend','replace','search','site_manage','special','statistics','tags','url_setting');
	        if(!empty($app_list)){
	        	foreach ($app_list as $value) {
	        		if(in_array($value, $app_no_list)){
	        			continue;
	        		}
	        		$app_config_info=appConfig($value);
	        		if(!$app_config_info['APP_INSTALL']){
	        			continue;
	        		}
	        		$db_dir = BASE_PATH . 'apps/' . $value . '/install_data/';
	        		if(is_dir($db_dir)){
	        			//导入数据库
				        $db = new Dbbak($data['DB_HOST'].':'.$data['DB_PORT'],$data['DB_USER'],$data['DB_PWD'],$data['DB_NAME'],'utf8',$db_dir);
				        $db->importSql('',$app_config_info['APP_ORIGINAL_PREFIX'],$data['DB_PREFIX']);
	        		}
	        	}
	        }
    	}

        //复制配置文件
        $default_tpl=ROOT_PATH.'inc/site/default_tpl';
        $new_tpl=ROOT_PATH.'inc/site/'.$data['SITE_MARK'];
        copy_dir($default_tpl, $new_tpl);
        //创建配置文件
        $config="<?php\nreturn array (\n  'SITE_NAME' => '".$data['SITE_NAME']."',\n  'SITE_DOMAIN' => '".$data['SITE_DOMAIN']."',\n);";
        $filename=ROOT_PATH.'inc/site/'.$data['SITE_MARK'].'/config.php';
        if(!@file_put_contents($filename, $config)){
        	del_dir($new_tpl,true);
        	$this->msg('新建站点失败，inc目录没有写入权限！',0);
        }
        //创建数据库配置
        $db_config='<?php 
		//数据库参数
        $config[\'DB\'][\'DB_HOST\']=\''.$data['DB_HOST'].'\';
		$config[\'DB\'][\'DB_PORT\']='.$data['DB_PORT'].';
		$config[\'DB\'][\'DB_USER\']=\''.$data['DB_USER'].'\';
		$config[\'DB\'][\'DB_PWD\']=\''.$data['DB_PWD'].'\';
		$config[\'DB\'][\'DB_NAME\']=\''.$data['DB_NAME'].'\';
		$config[\'DB\'][\'DB_PREFIX\']=\''.$data['DB_PREFIX'].'\';
        ';
        $db_filename=ROOT_PATH.'inc/site/'.$data['SITE_MARK'].'/db.ini.php';
        if(!@file_put_contents($db_filename, $db_config)){
        	del_dir($new_tpl,true);
        	$this->msg('新建站点失败，inc目录没有写入权限！',0);
        }
        //创建APP配置文件
        $sys_config='<?php 
		//基本参数
		$config[\'APP\'][\'COOKIE_PREFIX\']=\''.getcode(5).'_\';
		$config[\'APP\'][\'SAFE_KEY\']=\''.getcode(20).'\';
        ';
        $sys_filename=ROOT_PATH.'inc/site/'.$data['SITE_MARK'].'/app.ini.php';
        if(!@file_put_contents($sys_filename, $sys_config)){
        	del_dir($new_tpl,true);
        	$this->msg('新建站点失败，inc目录没有写入权限！',0);
        }
        //完成创建
        $this->msg('站点创建成功，站点默认管理帐号密码均为admin');
    }

    //修改
    public function edit() {
        $mark=$_GET['mark'];
        $this->alert_str($mark,'text');
        $this->info=model('site_manage')->info($mark);
        $this->action_name='编辑';
        $this->action='edit';
        $this->show('admin_list/info'); 
    }

    public function edit_save() {
        $data=in($_POST);
        $mark=$_POST['SITE_MARK'];
        $this->alert_str($mark,'text',true);
        //创建配置文件
        $config="<?php\nreturn array (\n  'SITE_NAME' => '".$data['SITE_NAME']."',\n  'SITE_DOMAIN' => '".$data['SITE_DOMAIN']."',\n);";
        $filename=ROOT_PATH.'inc/site/'.$data['SITE_MARK'].'/config.php';
        if(!@file_put_contents($filename, $config)){
            $this->msg('修改站点失败，inc目录没有写入权限！',0);
        }
        //创建数据库配置
        $db_config='<?php 
        //数据库参数
        $config[\'DB\'][\'DB_HOST\']=\''.$data['DB_HOST'].'\';
        $config[\'DB\'][\'DB_PORT\']='.$data['DB_PORT'].';
        $config[\'DB\'][\'DB_USER\']=\''.$data['DB_USER'].'\';
        $config[\'DB\'][\'DB_PWD\']=\''.$data['DB_PWD'].'\';
        $config[\'DB\'][\'DB_NAME\']=\''.$data['DB_NAME'].'\';
        $config[\'DB\'][\'DB_PREFIX\']=\''.$data['DB_PREFIX'].'\';
        ';
        $db_filename=ROOT_PATH.'inc/site/'.$data['SITE_MARK'].'/db.ini.php';
        if(!@file_put_contents($db_filename, $db_config)){
            $this->msg('修改失败，inc目录没有写入权限！',0);
        }
        $this->msg('站点修改成功! ',1);
    }

    //删除
    public function del() {
        $mark=in($_POST['mark']);
        $this->alert_str($mark,'text',true);
        if($mark=='default'){
            $this->msg('默认站点不可删除！',0);
        }
        $dir=ROOT_PATH.'inc/site/'.$mark;
        if(@del_dir($dir,true)){
            $this->msg('站点删除成功！',1);
        }else{
            $this->msg('站点删除失败，请检查inc目录是否有权限！',0);
        }
        
    }


}